package com.jky.trial.service.impl;

import com.jky.trial.common.BaseResult;
import com.jky.trial.common.ListResult;
import com.jky.trial.common.Pagination;
import com.jky.trial.entity.TaskPersonDetail;
import com.jky.trial.entity.TaskPersonDetailExample;
import com.jky.trial.mapper.TaskPersonDetailMapper;
import com.jky.trial.mapper.TaskPersonDetailMapper;
import com.jky.trial.service.TaskPersonService;
import com.jky.trial.vo.TaskPersonDetailVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class TaskPersonServiceImpl implements TaskPersonService {
    private final TaskPersonDetailMapper recordMapper;

    @Autowired
    TaskPersonServiceImpl(TaskPersonDetailMapper recordMapper) {
        this.recordMapper = recordMapper;
    }


    @Override
    public BaseResult create(TaskPersonDetail record) {

        record.setCreateAt(new Date());
        TaskPersonDetailExample example = new TaskPersonDetailExample();
        TaskPersonDetailExample.Criteria criteria = example.createCriteria();
//        criteria.andNameEqualTo(record.getName());
//        criteria.andDeletedEqualTo(false);//
        List<TaskPersonDetail> list = recordMapper.selectByExample(example);
        if (list.size() != 0) {
            return new BaseResult(false, "该勘察点名称已经存在，不能重复添加");
        }
        TaskPersonDetailExample example1 = new TaskPersonDetailExample();
        TaskPersonDetailExample.Criteria criteria1 = example1.createCriteria();
//        criteria1.andNoEqualTo(record.getNo());
//        criteria1.andDeletedEqualTo(false);//
        List<TaskPersonDetail> list1 = recordMapper.selectByExample(example1);
        if (list1.size() != 0) {
            return new BaseResult(false, "勘察点编号重复");
        }

        record.setCreateAt(new Date());
        recordMapper.insertSelective(record);
        return new BaseResult(true, record);
    }

    @Override
    public int update(TaskPersonDetail record) {
        TaskPersonDetailExample recordExample = new TaskPersonDetailExample();
        TaskPersonDetailExample.Criteria criteria = recordExample.createCriteria();
        criteria.andIdEqualTo(record.getId());//查询条件
        return recordMapper.updateByExampleSelective(record, recordExample);
    }

    @Override
    public int del(int id) {
        TaskPersonDetail task = new TaskPersonDetail();
        task.setId(id);
        return recordMapper.updateByPrimaryKey(task);
    }

    @Override
    public TaskPersonDetail detail(int id) {
        return recordMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<TaskPersonDetailVo> list(TaskPersonDetailVo record) {
        return recordMapper.pageList(record);
    }

    /**
     * 按项目ID勘察点
     *
     * @param record
     * @return
     */
    @Override
    public ListResult page(TaskPersonDetailVo record) {
        List<TaskPersonDetailVo> list = list(record);
        ListResult res = new ListResult(true, list);
        res.setPage(new Pagination(
                record.getPage(),
                (int) count(record),
                record.getSize()
        ));
        return res;
    }

    @Override
    public long count(TaskPersonDetailVo record) {
        TaskPersonDetailExample recordExample = new TaskPersonDetailExample();
        TaskPersonDetailExample.Criteria criteria = recordExample.createCriteria();
//        if (record.getProjectId() != null) criteria.andProjectIdEqualTo(record.getProjectId());
//        if (record.getTaskId() != null) criteria.andTaskIdEqualTo(record.getTaskId());
        if (record.getSearch() != null) { //按名称搜索
//            criteria.andNoLike(record.getSearch());
        }
        if (record.getStatus() != null) { //按状态筛选
//            criteria.andStatusEqualTo(record.getStatus());
        }
        return recordMapper.countByExample(recordExample);
    }
}
